package com.example.wto.viewpagertest01;//package com.wto;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ParseJsonUtil
{
	public static Teacher ParseFromTeacherJson(String json)
	{
			Teacher teacher=new Teacher();
            TeacherBean bean=new TeacherBean();
        try
        {
            JSONObject jsonObject=new JSONObject(json);
            bean.setCollege_id(jsonObject.getString("college_id"));
            bean.setCollge_name(jsonObject.getString("college_name"));
            bean.setS_id(jsonObject.getString("s_id"));
            bean.setS_name(jsonObject.getString("s_name"));
            bean.setTeacherID(jsonObject.getString("teacher_id"));
            bean.setTeacherName(jsonObject.getString("teacher_name"));

            teacher.setBean(bean);
            JSONArray coursesJson=jsonObject.getJSONArray("courses");
            List<DayOfWeek> courses=teacher.getWeekList();
            courses.clear();
            JSONObject weekDayJson=null;
            JSONArray  courseJson =null;
            DayOfWeek weekDay=null;
            //��ȡһ��7��Ŀα�
            for(int i=0;i<coursesJson.length();i++)
            {
                weekDayJson=coursesJson.getJSONObject(i);
                //��ȡ���ڼ�������
                weekDay=new DayOfWeek(weekDayJson.getString("weekName"));
                weekDay.getCourseList().clear();
                courseJson=weekDayJson.getJSONArray("course");
                //��ȡ���ڼ��ĵ�һ�ڿΣ��ڶ��ڿΡ�������
                for(int j=0;j<courseJson.length();j++)
                {
                    weekDay.getCourseList().add(new Course(courseJson.getString(j)));
                }
                courses.add(weekDay);
            }
        } catch (JSONException e)
        {
            e.printStackTrace();
        }

        return teacher;
	}
	public static List parseFromTeacherListJson(String json)
    {
        List<TeacherBean> list=new ArrayList<>();
        try
        {
            JSONArray jsonArray=new JSONArray(json);
            for(int i=0;i<jsonArray.length();i++)
            {
                TeacherBean bean=new TeacherBean();
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                bean.setTeacherID(jsonObject.getString("id"));
                bean.setTeacherName(jsonObject.getString("name"));
                list.add(bean);
            }
        } catch (JSONException e)
        {
            e.printStackTrace();
        }
        return list;
    }
	public static  List parseFromTermJson(String json)
    {
        List<TermBean> list=new ArrayList<>();
        try
        {
            JSONArray jsonArray=new JSONArray(json);
            for(int i=0;i<jsonArray.length();i++)
            {
                TermBean bean=new TermBean();
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                bean.setTermID(jsonObject.getString("id"));
                bean.setTermName(jsonObject.getString("name"));
                list.add(bean);
            }
        } catch (JSONException e)
        {
            e.printStackTrace();
        }
        return list;
    }
}
